package ru.spb.osll.myfit.client.widget;

import java.util.Vector;
import java.util.HashMap;

import ru.spb.osll.myfit.client.PageMode;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

/**
 * The includeWidget is created based on data from Include. The includeWidget is
 * a DecoratePanel, which contains tableWidgets and other IncludeWidgets.
 * 
 * @author Mariya Gavrilova
 * 
 */
public class IncludeWidget extends PageElementWidget {

  /**
   * included page
   */
  protected final Page includePage;

  /**
   * included page path
   */
  protected final String includePath;

  /**
   * widget, that content all other widgets on the page.
   */
  private PageWidget m_contentWidget;

  /**
   * upper panel.
   */
  private DisclosurePanel m_advancedDisclosure;

  protected final PageElementType m_type;

  /**
   * 
   * @param include
   * @param width
   *          - width of widget
   */
  public IncludeWidget(GWTWidgetFactory gwtWidgetFactory, Include include) {
    super(gwtWidgetFactory);
    sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    createPopupMenu();
    m_type = include.getType();
    includePage = include.getPage();
    includePath = include.getPath();
    initWidget(createContentWidget());
    m_mode = PageMode.EDIT;
  }

  /**
   * create widget, which will be added into a panel (DecoratorPanel)
   * 
   * @return Widget - created widget
   */
  protected Widget createContentWidget() {

    FlexTable layout = m_gwtWidgetFactory.createFlexTable();
    layout.setCellSpacing(6);
    Vector<PageElement> pageElements = includePage.getElements();
    int countPageElement = pageElements.size();
    m_contentWidget = new PageWidget(m_gwtWidgetFactory);
    for (int i = 0; i < countPageElement; i++) {
      m_contentWidget.addPageElementWidget(pageElements.get(i));
    }

    m_contentWidget.setMode(PageMode.VIEW);

    String includeTitle = getIncludeName() + " " + getIncludePath();

    m_advancedDisclosure = (DisclosurePanel) m_gwtWidgetFactory
        .createDisclosurePanel(includeTitle, m_contentWidget);
    layout.setWidget(0, 0, m_advancedDisclosure);

    DecoratorPanel panel = m_gwtWidgetFactory.createDecoratorPanel();

    panel.setWidget(layout);
    return panel;
  }

  protected String getIncludeName() {
    PageElementType type = getType();
    return type.getKey();
  }

  protected String getIncludePath() {
    return includePath;
  }

  /**
   * Changes the visible state of this DisclosurePanel. And does it recursively
   * to all sub_includes.
   * 
   * @param isOpen
   *          true to "open" the widget, false to close.
   */
  @Override
  public void setOpen(boolean isOpen) {
    m_advancedDisclosure.setOpen(isOpen);
    m_contentWidget.setOpen(isOpen);
  }

  public PageElement getPageElement() {
    return new Include(includePath, includePage);
  }

  public void setStyle() {
    m_advancedDisclosure.setStyleName("IncludeErrorPanel");
  }

  public PageElementType getType() {
    return m_type;
  }

  public PageWidget getPageWidget() {
    return m_contentWidget;
  }

  public HashMap<String, String> getVariables() {
    return includePage.getVariables();
  }

  public void setVariables(HashMap<String, String> map) {
    m_contentWidget.setVariables(map);
  }

  public void updateWidgets(HashMap<String, String> map) {
    if ((map != null) && (!map.isEmpty()))
      m_contentWidget.updateWidgets(map);
  }
}
